package com.hpplay.sdk.source.test.manager;

import com.hpplay.sdk.source.api.ILelinkPlayerListener;
import com.hpplay.sdk.source.test.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * 推送
 */
public class CastManager {
    private final static String TAG = "DemoCastManager";
    private static CastManager sInstance = null;
    private final List<ILelinkPlayerListener> mListenerList = new ArrayList<>();

    private final ILelinkPlayerListener mLelinkPlayerListener = new ILelinkPlayerListener() {

        @Override
        public void onLoading() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onLoading();
            }
        }

        @Override
        public void onStart() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onStart();
            }
        }

        @Override
        public void onPause() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onPause();
            }
        }

        @Override
        public void onCompletion() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onCompletion();
            }
        }

        @Override
        public void onStop() {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onStop();
            }
        }

        @Override
        public void onSeekComplete(int i) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onSeekComplete(i);
            }
        }

        @Override
        public void onInfo(final int what, final int extra) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onInfo(what, extra);
            }
        }

        @Override
        public void onInfo(int what, final String data) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onInfo(what, data);
            }
        }

        @Override
        public void onError(int what, int extra) {
            Logger.i(TAG, "onError what:" + what + " extra:" + extra);
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onError(what, extra);
            }
        }

        @Override
        public void onVolumeChanged(float v) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onVolumeChanged(v);
            }
        }

        @Override
        public void onPositionUpdate(long duration, long position) {
            for (ILelinkPlayerListener listener : mListenerList) {
                listener.onPositionUpdate(duration, position);
            }
        }
    };

    public synchronized static CastManager getInstance() {
        synchronized (CastManager.class) {
            if (sInstance == null) {
                sInstance = new CastManager();
            }
        }
        return sInstance;
    }

    private CastManager() {

    }

    public ILelinkPlayerListener getLelinkPlayerListener() {
        return mLelinkPlayerListener;
    }

    public void addPlayerListener(ILelinkPlayerListener listener) {
        mListenerList.add(listener);
    }

    public void removeListener(ILelinkPlayerListener listener) {
        mListenerList.remove(listener);
    }

}
